<?php
/**
* fingerscan.php
*/

include_once("ajax/config/config.inc.php");
$templatepath='C:\\\\dpfps\\\\fpt\\\\';

function getUsers() 
{
	$sql = " select username, fullname from users order by username ";
	$res = mysql_query($sql) or die(mysql_error());
	if(mysql_num_rows($res)) {
		
		$retval="<select name='user' id='user'>";
		while(list($username,$fullname)=mysql_fetch_row($res)) {
			$retval.="<option value='$username'>$fullname</option>";
		}
		$retval.="</select>";
	}
	return $retval;
}

if($_POST["act"]=='scan') {
	$fp = fopen("C:/dpfps/Verification/pool/validate.txt","w");
	fwrite($fp, $templatepath . $_POST['user'].".fpt");
	fclose($fp);
	passthru("C:\PsTools\psexec fscan.bat");
	
}elseif($_POST['act']=='monitor') {
	$resultfile = "C:/dpfps/Verification/pool/result.txt";
	$validatefile = "C:/dpfps/Verification/pool/validate.txt";
	if(file_exists($resultfile)) {
		$str = file_get_contents($resultfile);
		if(trim($str)=='verified') {
			$user = $_POST['user'];
			$sql = "select a.user_id, a.group_id, b.group_name, a.fullname 
				from users a, groups b
				where a.group_id=b.group_id
				and a.username=binary('$user') 
			";
			$res = mysql_query($sql) or die(JSON_encode(array('success'=>false,'msg'=>"Authentication $user failed. Please contact your administrator." . $sql)));
			if(mysql_num_rows($res)) {
				list($userid,$groupid,$groupname,$fullname)=mysql_fetch_row($res);
				session_start();
				$_SESSION["hotel"]["logged"]=true;
				$_SESSION["hotel"]["userid"]=$userid;
				$_SESSION["hotel"]["groupid"]=$groupid;
				$_SESSION["hotel"]["groupname"]=$groupname;
				$_SESSION["hotel"]["fullname"]=$fullname;
				$shiftstatus = checkShiftStarted();
				$msg[0]= 'Identity confirmed. Please perform shift start activities first.';
				$msg[1] = 'Identity confirmed. Welcome ' . $fullname;
				
				$resp = array('success'=>true, 'msg'=> $msg[$shiftstatus], 'shiftstatus'=>$shiftstatus);
				$json =  JSON_encode($resp);
				accessLogs($userid,"Login",$json);
				echo 'success';
			}
		}
		unlink($resultfile);
		unlink($validatefile);
	}
}

function checkShiftStarted() {
//place code to check shift status here
// 0 -> shift not started
// 1 -> shift started, go to index page
	return rand(0,1);
}
?>
<?php if(!$_POST) { ?>
<script type="text/javascript" src="js/jquery.js"></script>
<script language='javascript'>
$(document).ready(function(){
	$("#scan").click(function(){
		$("#processzone").html("Scanning...");
		$.post( "fingerscan.php",
			{act: 'scan', user: $('#user').val()}
		);
		setInterval(checkscan, 3000);
		return false;
	});
	
	function checkscan() {
		$.post( "fingerscan.php",
			{act: 'monitor', user: $('#user').val()},
			function(data) {
				if(data=='success') {
					document.location.href='index.php';
				}
			}
		);
	}
});
</script>
<form method="post" action="fingerscan.php">
<div align="center">
<h1>Shogun Fingerprint Verification</h1>
<label>Select Personnel:</label>
<?php echo getUsers() ?>
<br /><br />
<input type="submit" id='scan' name="scan" value="Begin Fingerprint Verification" />
<div id='processzone'></div>
</div>
</form>
<style>
label {font-size:20px}
#user {font-size:20px;}
input {font-size:20px}
</style>
<?php } ?>
